package com.dongdong.service.impl;

import com.dongdong.mapper.TbAddressMapper;
import com.dongdong.model.TbAddress;
import com.dongdong.model.TbAddressExample;
import com.dongdong.service.AddressService;
import com.dongdong.vo.AddressVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private TbAddressMapper addressMapper;

    @Override
    public List<AddressVO> getAddressByUserName(String username) {
        TbAddressExample example = new TbAddressExample();
        if (username == null) {
            return null;
        }
        example.createCriteria().andUserIdEqualTo(username);
        List<TbAddress> tbAddresses = addressMapper.selectByExample(example);
        List<AddressVO> collect = tbAddresses.stream().map(addresslist -> {
            AddressVO vo = new AddressVO();
            BeanUtils.copyProperties(addresslist, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public AddressVO getAddressById(Long addressId) {
        TbAddress tbAddress = addressMapper.selectByPrimaryKey(addressId);
        AddressVO vo = new AddressVO();
        BeanUtils.copyProperties(tbAddress,vo);
        return vo;
    }
}
